package com.tust.androidreader.Adapter;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tust.androidreader.BookActivity;
import com.tust.androidreader.DBHelper;
import com.tust.androidreader.LibraryActivity;
import com.tust.androidreader.R;

import java.util.List;
import java.util.Map;

class LibraryViewHolder extends RecyclerView.ViewHolder{
    private String userName;

    //成员变量为选项布局中要显示数据的视图组件
    ImageView bookicon;
    TextView bookname;
    TextView author;
    TextView likenum;
    TextView readtimes;
    TextView commentnum;
    TextView classify;
    TextView addtime;
    TextView wordnum;

    LinearLayout linearLayoutBookNameDiv;

    public LibraryViewHolder(@NonNull View itemView, String userName) {
        super(itemView);

        this.userName = userName;

        this.bookicon = (ImageView)itemView.findViewById(R.id.iv_bookicon);
        this.bookname = (TextView)itemView.findViewById(R.id.tv_bookname);
        this.author = (TextView)itemView.findViewById(R.id.tv_author);
        this.likenum = (TextView)itemView.findViewById(R.id.tv_likenum);
        this.readtimes = (TextView)itemView.findViewById(R.id.tv_readtimes);
        this.commentnum = (TextView)itemView.findViewById(R.id.tv_commentnum);
        this.classify = (TextView)itemView.findViewById(R.id.tv_classify);
        this.addtime = (TextView)itemView.findViewById(R.id.tv_addtime);
        this.wordnum = (TextView)itemView.findViewById(R.id.tv_wordnum);

        this.linearLayoutBookNameDiv = (LinearLayout)itemView.findViewById(R.id.linear_booknamediv);

        this.bookicon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),bookname.getText().toString(),Toast.LENGTH_SHORT).show();
            }
        });

        this.linearLayoutBookNameDiv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String bookName = bookname.getText().toString();
                Toast.makeText(v.getContext(),"正在删除《" + bookName + "》",Toast.LENGTH_SHORT).show();

                Context context = itemView.getContext();
                DBHelper dbHelper = new DBHelper(context);
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                db.execSQL("delete from book where bookname = ?", new Object[]{bookName});
                db.execSQL("delete from comment where bookname = ?", new Object[]{bookName});

                //刷新LibraryActivity
                Intent intent = new Intent(context, LibraryActivity.class);
                context.startActivity(intent);
                return false;
            }
        });

        this.linearLayoutBookNameDiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String bookName = bookname.getText().toString();
                Toast.makeText(v.getContext(),"正在打开《" + bookName + "》",Toast.LENGTH_SHORT).show();

                Context context = itemView.getContext();
                Intent intent = new Intent(context, BookActivity.class);
                intent.putExtra("bookname", bookName);
                intent.putExtra("username", userName);
                context.startActivity(intent);
            }
        });
    }
}

public class LibraryRecyclerAdapter extends RecyclerView.Adapter<LibraryViewHolder> {
    private Context context;
    private List<Map<String,Object>> bookList;
    private String userName;

    public LibraryRecyclerAdapter(Context context, List<Map<String, Object>> list, String userName) {
        this.context = context;
        this.bookList = list;
        this.userName = userName;
    }

    @NonNull
    @Override
    public LibraryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_library_recyclerview,parent,false);
        LibraryViewHolder libraryViewHolder = new LibraryViewHolder(view, userName);

        libraryViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("COURSE","viewType=>"+libraryViewHolder.getAdapterPosition());
            }
        });

        return libraryViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull LibraryViewHolder holder, int position) {
        Map<String,Object> itemdata = bookList.get(position);

        holder.bookicon.setImageResource((Integer)itemdata.get("bookicon"));
        holder.bookname.setText((String)itemdata.get("bookname"));
        holder.author.setText((String)itemdata.get("author"));
        holder.likenum.setText((String)itemdata.get("likenum"));
        holder.readtimes.setText((String)itemdata.get("readtimes"));
        holder.commentnum.setText((String) itemdata.get("commentnum"));
        holder.classify.setText((String)itemdata.get("classify"));
        holder.addtime.setText((String)itemdata.get("addtime"));
        holder.wordnum.setText((String)itemdata.get("wordnum"));
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }
}